/* eslint-disable no-fallthrough */
/**
 * @author tlm
 */

import JSZip from 'jszip';
import JSZipUtils from 'jszip-utils';

import {WipAttLoader} from './WipAttLoader';

import ComponentSignals from '@/utils/componentSignals';


class FileLoader {
    constructor() {
        this.texturePath = '';
        
        this.loadFiles = (files)=> {
            if (files.length > 0) {
                for (let i = 0; i < files.length; i++) {
                    this.loadFile(files[i]);
                }
            }
        };

        this.loadFile =(file)=> {
            if (file === null) {
                return;
            }
            if (file instanceof Object) {
                let filename = file.name;
                let extension = filename.split('.').pop().toLowerCase();
                let reader = new FileReader();
                                           
                ComponentSignals.loaderSegment.dispatch(true);
                switch (extension) {
                case 'xml':
                    reader.addEventListener('load', function (event) {
                        let contents = event.target.result;
                        let jsData = WipAttLoader(contents);
                        ComponentSignals.setAttribute.dispatch(jsData);
                        ComponentSignals.loaderSegment.dispatch(false);
                    }, false);
                    reader.readAsText(file);
                    break;
                case 'spd':
                case 'zip':
                    reader.addEventListener('load', function (event) {
                        handleZIP(event.target.result);
                        ComponentSignals.loaderSegment.dispatch(false);
                    }, false);
                    reader.readAsBinaryString(file);
                    break;
                case 'json': 
                    ComponentSignals.loadGeo.dispatch(file);
                    break;
                case 'gltf': 
                case 'obj':
                case 'stl':
                    ComponentSignals.loadGeo.dispatch(file);
                    break;
                default:
                    ComponentSignals.loaderSegment.dispatch(false);
                    break;
                }
            }
            else {
                let extension = file.split('.').pop().toLowerCase();
                if (extension !== 'zip') {
                    return;
                }
                let promise = new JSZip.external.Promise(function (resolve, reject) {
                    JSZipUtils.getBinaryContent(file, function (err, data) {
                        if (err) {
                            reject(err);
                        }
                        else {
                            resolve(data);
                        }
                    });
                });
                promise.then(handleZIP);
            }
        };

        function handleZIP(contents) {
            JSZip.loadAsync(contents).then(function (zip) {
                zip.forEach(function (relativePath, zipEntry) {
                    let extension = zipEntry.name.split('.').pop().toLowerCase();
                    switch (extension) {
                    case 'xml':
                        zip.file(zipEntry.name).async('string').then(
                            function (data) {
                                let jsData = WipAttLoader(data);
                                ComponentSignals.setAttribute.dispatch(jsData);
                            }
                        );
                        break;
                    case 'json':
                        zip.file(zipEntry.name).async('string').then(
                            function (data) {
                                ComponentSignals.loadGeoBlobData.dispatch(data);
                            });

                        break;
                    default:
                        break;
                    }

                    ComponentSignals.loaderSegment.dispatch(false)
                });
                
            });
        }
    }
}
export {FileLoader}
